package com.adcc.pushXml.server;

import com.adcc.pushXml.constant.Constant;
import com.adcc.pushXml.core.FlightPos;
import com.adcc.pushXml.utils.Utils;
import com.alibaba.fastjson.JSON;
import org.apache.activemq.command.ActiveMQBytesMessage;

import org.apache.activemq.util.ByteSequence;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author LHB
 * @date 2019/11/21 18:33
 */
@Service
public class MonitorMessages {
    private Logger log = LoggerFactory.getLogger(MonitorMessages.class);
    @Autowired
    private ExecuteParseMsg executeParseMsg;

    /**
     * 接受飞机报文
     */
    @JmsListener(destination = "${spring.activemq.topicName}")
    public void recvMsg(Object message) {
        if (message == null) {
            log.info("Flight message is null");
        }
        try {
            if (message instanceof ActiveMQBytesMessage) {
                ActiveMQBytesMessage msg = (ActiveMQBytesMessage) message;
                ByteSequence msgByte = msg.getMessage().getContent();
                String content = new String(msgByte.getData());
                log.info("recv messages : [{}]", content);
                if (StringUtils.isEmpty(content)) {
                    return;
                }
                //封装msg
                FlightPos flight = JSON.parseObject(content, FlightPos.class);
                //计算航向角
                FlightPos oldFlight = Constant.recvMsgs.get(flight.getFi());
                if (oldFlight != null) {
                    Double angle = Utils.getAngle1(oldFlight.getLat(), oldFlight.getLon(), flight.getLat(), flight.getLon());
                    flight.setAngle(angle);
                }
                //存入map
                Constant.recvMsgs.put(flight.getFi(),flight);
                //进行解析解析后进行发送
                executeParseMsg.parseXmlByMsg(flight);
            } else {
                log.info("recv messages : [{}]", message);
            }
        } catch (Exception e) {
            log.error("message parse error", message);
        }

    }


}
